package utility;


import java.sql.ResultSet;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import control.RequestParameter;
import pool.bean.Master_table_Bean;
import pool.bean.Property_details_Bean;
import property.Property;
import dbmanager.DBManager;
import dbmanager.GetIdName;

public class EventModification
{
	DBManager database;
	Hashtable<Object, Object> table = new Hashtable<Object, Object>();
	Hashtable<Object, Object> Index= new Hashtable<Object, Object>();
	GetIdName gid; 
	String TYPE="type";
	int add=1;
	int mod=1;
	int view=1;
	int del=1;
	
	public void Setdata(DBManager database,Hashtable<Object, Object> table)
	{
		this.database = database;
		this.table.putAll(ConvertToLower.convertHashKey(table));
		gid = new GetIdName(database);
		TYPE = gid.getId(TYPE);
		
	}
	
	public void getresult(String Event,String Event1)
	{
		Hashtable<Object, Object> eventhashtable = new Hashtable<Object, Object>();
		try 
		{
			ResultSet rs =database.getSelect("select * from property_details where pid='"+Event + "' and td is null  order by mid,ref" );
			database.getUpdate("update property_details set td=fd  where pid='"+Event + "'");
			while(rs.next())
			{
				String mid=rs.getString("mid");
				if(!eventhashtable.containsKey(mid))
					eventhashtable.put(mid, new Vector<Object>());
				Vector<Object> vector = (Vector<Object>) eventhashtable.get(mid);
				vector.add(rs.getString("pv"));
				eventhashtable.put(mid, vector);
			}
			Enumeration<Object> enumer = eventhashtable.keys();
			RequestParameter requestparameter = new RequestParameter();
			requestparameter.setUsername("+911234567891");
			requestparameter.setPassword("891");
			requestparameter.setProcess("add");
			
			while(enumer.hasMoreElements())
			{
				String key=enumer.nextElement().toString();
				Vector<Object> event = (Vector<Object>) eventhashtable.get(key);
				String eventname = gid.getItem(key);
				String type = "process_" +eventname;
				//type = "event_" +type;
				Hashtable<Object, Object> MasterMap = new Hashtable<Object, Object>();
				MasterMap.put("name", type);
				Hashtable<Object, Object> PropertyMap = new Hashtable<Object, Object>();
				PropertyMap.put("mid", type);
				PropertyMap.put("pid", "type");
				PropertyMap.put("pv", Event1);
				Master_table_Bean master = new Master_table_Bean();
				master.setDBManager(database);
				master.setHashtable(MasterMap);
				master.setPropertyHash(PropertyMap);
				PropertyMap.clear();
				PropertyMap.put("mid", gid.getItem(key));
				PropertyMap.put("pid", Event1);
				PropertyMap.put("pv", type);
				requestparameter.setRequest("master_table");
				master.setReqParam(requestparameter);
				master.insert();
				
				requestparameter.setRequest("property_details");
				Property_details_Bean property_details =new Property_details_Bean();
				property_details.setDBManager(database);
				property_details.setReqParam(requestparameter);
				property_details.setPropertyHash(PropertyMap);
				property_details.setMid(eventname);
				property_details.insert();
				
				Index.put("add", "1");
				Index.put("mod", "1");
				Index.put("del", "1");
				Index.put("view", "1");
				for(int i=0;i<event.size();i++)
				{
					String eventname1 =((event.get(i).toString()));
					Property property = new Property(database);
					property.setMid(eventname1);
					eventname1 = gid.getItem(eventname1);
					property.setPid(TYPE);
					String mastertype =property.getValue().toLowerCase();
					if(table.containsKey(mastertype))
					{
							String process=table.get(mastertype).toString();
							String splitprocess[] =process.split(",");
							for(int j=0;j<splitprocess.length;j++)
							{
								PropertyMap.clear();
								PropertyMap.put("mid", type);
								PropertyMap.put("pid",splitprocess[j]);
								PropertyMap.put("pv", eventname1);
								if(Index.containsKey(splitprocess[j].toLowerCase()))
								{
									PropertyMap.put("ref", Index.get(splitprocess[j].toLowerCase()));
									Index.put(splitprocess[j].toLowerCase(),Integer.toString(Integer.parseInt(Index.get(splitprocess[j].toLowerCase()).toString())+1));
								}	
								else
									PropertyMap.put("ref", "1");
								requestparameter.setRequest("master_table");
								Property_details_Bean property_details1 =new Property_details_Bean();
								property_details1.setDBManager(database);
								property_details1.setReqParam(requestparameter);
								property_details1.setPropertyHash(PropertyMap);
								property_details1.setMid(type);
								property_details1.insert();
							}
					}
				}
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) 
	{
		Hashtable<Object, Object> table = new Hashtable<Object, Object>();
		table.put("defaultvalue","add");
		table.put("duplicateproperty","add");
		table.put("getproperty","add");
		table.put("operation","add,mod,del");
		table.put("validation","add,mod,del");
		table.put("getrate","add,mod,del");
		
		EventModification event  = new EventModification();
		DBManager database= new DBManager();
		database.getConnect();
		event.Setdata(database, table);
		String master="process";
		GetIdName gid = new GetIdName(database);
		String newmaster = gid.getId(master);
		event.getresult(newmaster,master);
	}
	
}




